package com.thread;

import java.util.concurrent.CountDownLatch;

/**
 * @author yinxing
 * @date 2022-08-31
 */
public class ThreadTest {

    private static Integer number = 0;
    private static Integer number1 = 128;
    private static CountDownLatch countDownLatch = new CountDownLatch(2);

    public static void main(String[] args) throws InterruptedException {
        ThreadTest threadTest =new ThreadTest();
        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                try {
                    //integer有缓存，-128~127
                    threadTest.test(number);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        countDownLatch.await();
        System.out.println("最终值" + number);
    }


    public  void test(Integer id) throws InterruptedException {
        for (int i = 0; i < 10000; i++) {
            synchronized (number) {
                System.out.println("拿到锁" + System.identityHashCode(number));
//                Thread.sleep(1000);

                System.out.println("准备释放锁" + System.identityHashCode(number++));
            }
        }
    }

}
